package com.dlwx.firefighting.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dlwx.firefighting.R;
import com.dlwx.firefighting.bean.ClassifyBean;
import com.dlwx.firefighting.listener.RecyclerviewItemClickListener;
import com.dlwx.firefighting.utils.ScreenUtils;

import java.util.List;

/**
 * Created by Administrator on 2018/3/17.
 */

public class GridClassifyAdapter extends RecyclerView.Adapter<GridClassifyAdapter.GridViewHolder> implements View.OnClickListener {

    private final int width;
    private Context context;
    private List<ClassifyBean> datas;
    RecyclerviewItemClickListener listener;
    public GridClassifyAdapter(Context context, List<ClassifyBean> datas,  RecyclerviewItemClickListener listener) {
        this.context=context;
        this.datas=datas;
        this.listener=listener;
        width=ScreenUtils.getScreenWidth(context);
    }

    @Override
    public GridViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_classify,parent,false);
        view.getLayoutParams().width=width/4;
        GridViewHolder viewHolder=new GridViewHolder(view);
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(GridViewHolder holder, int position) {
        ClassifyBean bean = datas.get(position);
        Glide.with(context).load(bean.getIcon()).error(R.mipmap.logo_bg).into(holder.imageView);
        holder.tv_name.setText(bean.getName());
        holder.itemView.setTag(position);
    }


    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public void onClick(View v) {
        listener.onItemClickListener(v, (Integer) v.getTag());
    }

    public static  class GridViewHolder extends RecyclerView.ViewHolder{
        private ImageView imageView;
        private TextView tv_name;
        public GridViewHolder(View itemView) {
            super(itemView);
            imageView= (ImageView) itemView.findViewById(R.id.image);
            tv_name= (TextView) itemView.findViewById(R.id.tv_name);
        }
    }
}
